package com.invocation.impl;

import com.interceptor.MethodInterceptor;
import com.invocation.MethodInvocation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class DefaultMethodInvocation implements MethodInvocation {

    ArrayList<MethodInterceptor> methodInterceptorArrayList;

    static int index;

    Method method;

    Object object;

    public DefaultMethodInvocation(ArrayList<MethodInterceptor> methodInterceptorArrayList, Method method, Object object) {
        this.methodInterceptorArrayList = methodInterceptorArrayList;
        this.method = method;
        this.object = object;
    }

    public DefaultMethodInvocation(ArrayList<MethodInterceptor> methodInterceptorArrayList) {
        this.methodInterceptorArrayList = methodInterceptorArrayList;
    }

    @Override
    public void process() throws InvocationTargetException, IllegalAccessException {
        if(index == methodInterceptorArrayList.size()){
            //执行目标方法
            method.invoke(object);
            return;
        }
        //调用添加进去的通知
        MethodInterceptor methodInterceptor = methodInterceptorArrayList.get(index);
        index++;
        methodInterceptor.invoke(this);
        //递归循环调用
//        process(this);
    }
}
